package smcms.service.sso.service.impl;

import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import smcms.commons.api.entity.result.CommonsResult;
import smcms.commons.api.entity.util.JwtUtil;
import smcms.service.sso.entity.StaffInfoExport;
import smcms.service.sso.entity.StaffInfoReq;
import smcms.service.sso.entity.StaffInfoResp;
import smcms.service.sso.mapper.StaffMapper;
import smcms.service.sso.service.ResourceService;
import smcms.service.sso.utils.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private CommonsResult commonsResult;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private StaffMapper staffMapper;
    @Override
    public CommonsResult exportStaffInfo(StaffInfoReq req, HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseToken(token);
        req.setCorporationId((String) claims.get("cloudId"));
        List<StaffInfoExport> staffInfoResps = staffMapper.exportCloudStaff(req);
        String[] rowName = {"姓名", "账号", "入职时间", "角色", "上级领导账号", "上级领导姓名", "性别", "手机号", "工资", "状态", "所在城市", "所属小店"};
        IOUtils.exportExcel(staffInfoResps,"员工信息表", response, StaffInfoExport.class, Arrays.asList(rowName));
        return commonsResult.success();
    }
}
